package com.douqu.game.main.client;


//import com.douqu.game.core.entity.ext.AltarInfo;
import com.douqu.game.core.protobuf.*;
import com.douqu.game.core.util.BufferUtils;
import com.douqu.game.core.util.SendUtils;
import com.douqu.game.core.util.CoreUtils;
//import com.douqu.game.main.server.GameServer;
        import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

/**
* Created by Administrator on 2016/11/9.
*/
public class NettyTCPHandler extends ChannelInboundHandlerAdapter {

    Logger logger = Logger.getLogger(this.getClass());


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        int code = BufferUtils.readShort(byteBuf);

        logger.info("data length:" + byteBuf.readableBytes());
        logger.info("code:"+code);

        try{
            byte[] data = CoreUtils.byteBufToBytes(byteBuf);

            switch (code) {
               /* case  SGMainProto.E_MSG_ID.MsgID_System_NotifyAlert_VALUE:
                    SGSystemProto.S2C_NotifyAlert res = SGSystemProto.S2C_NotifyAlert.parseFrom(data);
                    logger.info("error:"+ res.getType());
                    break;
                case  SGMainProto.E_MSG_ID.MsgID_System_SuperLogin_VALUE:
//                    SGBagProto.C2S_CardLevelUp.Builder b = SGBagProto.C2S_CardLevelUp.newBuilder();
//                    b.setSid(1);
                    SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Bag_MyCard_VALUE, null);
                    break;
//                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardLevelUp_VALUE://升级
//                    SGBagProto.S2C_CardLevelUp r = SGBagProto.S2C_CardLevelUp.parseFrom(data);

//                    logger.info("升级成功返回数据：" + r.toString());
//                    SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Bag_MyCard_VALUE, null);
//                    break;
                case  SGMainProto.E_MSG_ID.MsgID_Bag_MyCardTeam_VALUE://我的背包

                    SGBagProto.S2C_MyCardList r = SGBagProto.S2C_MyCardList.parseFrom(data);

                    logger.info("我的背包列表：" + r.toString() );
                    break;*/

//                case  SGMainProto.E_MSG_ID.MsgID_Pub_LotteryInit_VALUE://我的背包
//
//                    SGPlayerProto.S2C_LotteryInit rs = SGPlayerProto.S2C_LotteryInit.parseFrom(data);
//                    logger.info("酒馆初始化：" + rs.toString() );
//                    break;
//
//                case  SGMainProto.E_MSG_ID.MsgID_Bag_MyCardTeam_VALUE://我的背包
//
//                    SGBagProto.S2C_MyCardTeam r = SGBagProto.S2C_MyCardTeam.parseFrom(data);
//
//                    logger.info("我的卡组列表：" + r.toString() );
//                    break;
//
//
//                case  SGMainProto.E_MSG_ID.MsgID_Pub_LotteryClick_VALUE://我的背包
//
//                    SGPlayerProto.S2C_LotteryClick s = SGPlayerProto.S2C_LotteryClick.parseFrom(data);
//                    logger.info("抽奖返回数据：" + s.toString() );
//                    break;

//                case  SGMainProto.E_MSG_ID.MsgID_Altar_Init_VALUE://我的背包
//
//                    SGPlayerProto.S2C_AltarInit altarInit = SGPlayerProto.S2C_AltarInit.parseFrom(data);
//
//                    logger.info("祭坛初始化：" + altarInit.toString() );
//                    break;

//                case  SGMainProto.E_MSG_ID.MsgID_Altar_Sacrifice_VALUE://我的背包
//
//                    SGPlayerProto.S2C_Sacrifice sacrifice = SGPlayerProto.S2C_Sacrifice.parseFrom(data);
//
//                    logger.info("祭祀信息：---------" );
//
//
//                    System.out.println(sacrifice.toString());
//                    break;
//
//                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardDetail_VALUE://卡牌详情
//
//                    SGBagProto.S2C_CardDetail cardDetail = SGBagProto.S2C_CardDetail.parseFrom(data);
//
//                    logger.info("卡牌详情信息：---------" );
//
//
//                    System.out.println(cardDetail.toString());
//                    break;

//                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardUpLv_VALUE://卡牌详情
//
//                    SGBagProto.S2C_CardUpLv cardUpLv = SGBagProto.S2C_CardUpLv.parseFrom(data);
//
//                    logger.info("卡牌升级信息：---------" );
//
//
//                    System.out.println(cardUpLv.toString());
//                    break;

//                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardUpStar_VALUE://卡牌详情
//
//                    SGBagProto.S2C_CardUpStar cardUpStar = SGBagProto.S2C_CardUpStar.parseFrom(data);
//
//                    logger.info("卡牌升星信息：---------" );
//
//
//                    System.out.println(cardUpStar.toString());
//                    break;

 /*               case  SGMainProto.E_MSG_ID.MsgID_Bag_CardAddExp_VALUE://卡牌详情

                    SGBagProto.S2C_CardAddExp cardAddExp = SGBagProto.S2C_CardAddExp.parseFrom(data);

                    logger.info("卡牌添加经验卷轴信息：---------" );
                    System.out.println(cardAddExp.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardFate_VALUE://卡牌详情

                    SGBagProto.S2C_CardFate cardFate = SGBagProto.S2C_CardFate.parseFrom(data);

                    logger.info("卡牌宿命信息：---------" );
                    System.out.println(cardFate.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_CardFateList_VALUE://卡牌详情

                    SGBagProto.S2C_CardFateList cardFateList = SGBagProto.S2C_CardFateList.parseFrom(data);

                    logger.info("卡牌宿命列表信息：---------" );
                    System.out.println(cardFateList.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_AccessoryUp_VALUE://卡牌详情

                    SGBagProto.S2C_AccessoryUp accessoryUp = SGBagProto.S2C_AccessoryUp.parseFrom(data);

                    logger.info("饰品进阶信息：---------" );
                    System.out.println(accessoryUp.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_AccessoryIntensify_VALUE://卡牌详情

                    SGBagProto.S2C_AccessoryIntensify accessoryIntensify = SGBagProto.S2C_AccessoryIntensify.parseFrom(data);

                    logger.info("饰品强化信息：---------" );
                    System.out.println(accessoryIntensify.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_EquipIntensify_VALUE://卡牌详情

                    SGBagProto.S2C_EquipIntensify equipIntensify = SGBagProto.S2C_EquipIntensify.parseFrom(data);

                    logger.info("装备强化信息：---------" );
                    System.out.println(equipIntensify.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Bag_EquipUp_VALUE://卡牌详情

                    SGBagProto.S2C_EquipUp equipUp = SGBagProto.S2C_EquipUp.parseFrom(data);

                    logger.info("装备合成信息：---------" );
                    System.out.println(equipUp.toString());
                    break;

                case  SGMainProto.E_MSG_ID.MsgID_Mail_BlackList_VALUE://黑名单列表
                    logger.info("黑名单：---------");
                    SGPlayerProto.S2C_MailBlackList bl = SGPlayerProto.S2C_MailBlackList.parseFrom(data);
                    System.out.println(bl.toString());
                    break;*/
                case  SGMainProto.E_MSG_ID.MsgID_Instance_RequestLevelBattle_VALUE://副本发起挑战
                    logger.info(" 副本发起挑战：---------");
                    SGChallengeProto.S2C_RequestLevelBattle rs  = SGChallengeProto.S2C_RequestLevelBattle.parseFrom(data);
                    break;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();// 将消息发送队列中的消息写入到SocketChannel中发送给对方。
        logger.info("flush");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        super.channelActive(ctx);
        SGSystemProto.C2S_Login.Builder data = SGSystemProto.C2S_Login.newBuilder();
        data.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK);
        System.out.println("account:" + String.valueOf(NettyTCPClient.account ++ ));
        data.setAccount(String.valueOf(NettyTCPClient.account ++ ));
        data.setPassword("123456");
        data.setNormal(true);
        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_Login_VALUE, data.build().toByteArray());
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_SuperLogin_VALUE, null);



        //升级
//        SGBagProto.C2S_CardLevelUp.Builder b = SGBagProto.C2S_CardLevelUp.newBuilder();
//        b.setSid(1);
//
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Bag_CardLevelUp_VALUE, null);



//        SGPlayerProto.C2S_LotteryClick.Builder c = SGPlayerProto.C2S_LotteryClick.newBuilder();
//        c.setType(1);
//        c.setBuyType(1);
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Pub_LotteryClick_VALUE, c.build().toByteArray());

//        logger.info("客户端连接成功");
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Pub_LotteryInit_VALUE, null);


//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Altar_Init_VALUE, null);
//
//        SGPlayerProto.C2S_Sacrifice.Builder b = SGPlayerProto.C2S_Sacrifice.newBuilder();
//        b.setType(1);
//
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Altar_Sacrifice_VALUE, b.build().toByteArray());

//        b.setSid(1);
//
//        SGBagProto.C2S_CardDetail.Builder b = SGBagProto.C2S_CardDetail.newBuilder();
//        b.setCardId(1101);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardDetail_VALUE,b.build().toByteArray());


//        SGBagProto.C2S_MyCardTeam.Builder b = SGBagProto.C2S_MyCardTeam.newBuilder();
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_MyCardTeam_VALUE,b.build().toByteArray());


//        SGBagProto.C2S_CardUpLv.Builder cs = SGBagProto.C2S_CardUpLv.newBuilder();
//        cs.setCardId(1101);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardUpLv_VALUE,cs.build().toByteArray());

//        SGBagProto.C2S_CardUpStar.Builder b =  SGBagProto.C2S_CardUpStar.newBuilder();
//        b.setCardId(1101);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardUpStar_VALUE,b.build().toByteArray());

//         SGBagProto.C2S_CardAddExp.Builder b = SGBagProto.C2S_CardAddExp.newBuilder();
//        b.setCardId(1101);
//        b.setExpId(41);
//        b.setCount(10);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardAddExp_VALUE,b.build().toByteArray());

//        SGBagProto.C2S_CardFate.Builder b = SGBagProto.C2S_CardFate.newBuilder();
//        b.setCardId(1102);
//        b.setFateId(2);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardFate_VALUE,b.build().toByteArray());
//
//        SGBagProto.C2S_CardFateList.Builder b1 = SGBagProto.C2S_CardFateList.newBuilder();
//        b1.setCardId(1102);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_CardFateList_VALUE,b1.build().toByteArray());
//

//        SGBagProto.C2S_AccessoryUp.Builder b = SGBagProto.C2S_AccessoryUp.newBuilder();
//        b.setCardId(1101);
//        b.setAccessoryTypeValue(SGCommonProto.E_ACCESSORY_TYPE.ACCESSORY_TYPE_1_VALUE);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_AccessoryUp_VALUE,b.build().toByteArray());

//        SGBagProto.C2S_AccessoryIntensify.Builder b = SGBagProto.C2S_AccessoryIntensify.newBuilder();
//        b.setCardId(1101);
//        b.setAccessoryType(SGCommonProto.E_ACCESSORY_TYPE.forNumber(1));
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_AccessoryIntensify_VALUE,b.build().toByteArray());

//          SGBagProto.C2S_EquipIntensify.Builder b =   SGBagProto.C2S_EquipIntensify.newBuilder();
//          b.setCardId(1101);
//          b.setEquipType(SGCommonProto.E_EQUIP_TYPE.forNumber(1));
//          b.setCount(2);
//          SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_EquipIntensify_VALUE,b.build().toByteArray());

//        SGBagProto.C2S_EquipUp.Builder b = SGBagProto.C2S_EquipUp.newBuilder();
//        b.setCardId(1101);
//        b.setEquipType(SGCommonProto.E_EQUIP_TYPE.forNumber(1));
//                  SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Bag_EquipUp_VALUE,b.build().toByteArray());

//          SGPlayerProto.C2S_LotteryClick.Builder b = SGPlayerProto.C2S_LotteryClick.newBuilder();
//        b.setBuyType(1);
//        b.setType(1);
//        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Pub_LotteryClick_VALUE,b.build().toByteArray());

        SGChallengeProto.C2S_RequestLevelBattle.Builder request =  SGChallengeProto.C2S_RequestLevelBattle.newBuilder();
        request.setLevelId(1);
        request.setChapterId(1);
        SendUtils.sendMsg(ctx.channel(),SGMainProto.E_MSG_ID.MsgID_Instance_RequestLevelBattle_VALUE,request.build().toByteArray());

    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

        super.channelInactive(ctx);

        logger.info("客户端断开连接");

        ctx.close();

    }


}